<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryModel.name" placeholder="菜单名称" class="filter-item" style="width: 200px;" clearable />

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleEdit(0)">
        新增
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      row-key="id"
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" width="70" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="图标" prop="icon">
        <template slot-scope="scope">
          <i :class="scope.row.icon || ''" style="font-size: 20px;" />
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === '0'" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.type === '1'" size="small" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.type === '2'" size="small" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="url" prop="url" />
      <el-table-column label="授权标识" prop="permission" />
      <el-table-column label="操作" width="320">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" size="mini" type="button" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button icon="el-icon-plus" size="mini" type="button" @click="handleAddChild(scope.row.id)">新增子菜单</el-button>
          <el-button icon="el-icon-edit" size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-if="showDialog" :title="dialogTitle" width="40%" :visible.sync="showDialog">
      <Form ref="dataForm" :form-id="formId" @submitSuccess="showDialog=false; fetchList()" />
      <div slot="footer">
        <el-button @click.native="showDialog = false">取消</el-button>
        <el-button type="primary" @click.native="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Form from './form'
export default {
  components: { Form },
  data() {
    return {
      list: [],
      dialogTitle: '',
      listLoading: false,
      showDialog: false,
      formId: 0,
      queryModel: {
        username: null,
        nickname: null
      }
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      this.$api.menu.listTree(this.queryModel).then(res => {
        this.listLoading = false
        this.list = res.data
      })
    },
    handleFilter() {
      this.fetchList()
    },
    handleEdit(id) {
      this.showDialog = true
      this.formId = id
      this.dialogTitle = id ? '编辑' : '新增'
    },
    submitForm() {
      this.$refs.dataForm.submitForm()
    },
    handleAddChild(parentId) {
      this.showDialog = true
      this.formId = 0
      this.dialogTitle = '新增子菜单'
      // dom渲染完成后再调用方法，不然提示找不到dom
      this.$nextTick(() => {
        this.$refs.dataForm.getParentMenu(parentId)
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$api.menu.remove(id).then(res => {
          this.fetchList()
        })
      }).catch(() => {
      })
    }
  }
}

</script>
<style scoped>
</style>
